package cn.ycxy.lbl.posts.controller;

import cn.ycxy.lbl.common.exception.BusinessException;
import cn.ycxy.lbl.common.exception.enums.ResponseEnum;
import cn.ycxy.lbl.common.restful.R;
import cn.ycxy.lbl.pojo.posts.constant.DiscussPostsConstant;
import cn.ycxy.lbl.pojo.posts.domain.LblCsDiscussPost;
import cn.ycxy.lbl.posts.service.IDiscussPostsService;
import cn.ycxy.lbl.posts.service.ILikeService;
import cn.ycxy.lbl.posts.utils.SecurityContextUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "帖子点赞模块", value = "帖子点赞模块")
@RestController
public class LikeController implements DiscussPostsConstant {

    @Autowired
    private ILikeService likeService;


    @ApiOperation(value = "帖子点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "点赞实体类型", name="entityType", example = "2", required = true),
            @ApiImplicitParam(value = "点赞实体id", name="entityId", example = "1", required = true)
    })
    @PostMapping ("/like")
    @PreAuthorize("hasRole('user')")
    public R<Void> postsLike(@RequestBody Map<String, String> map){
        if (map.get("entityType") == null || map.get("entityId") == null){
            throw new BusinessException(ResponseEnum.BAD_REQUEST);
        }
        //获取登录用户id
        Long userId = SecurityContextUtils.getUserId();
        if (userId == 0){
            userId = 2L;
        }
        int entityType = Integer.parseInt(map.get("entityType"));
        int entityId = Integer.parseInt(map.get("entityId"));
        likeService.like(userId, entityType, entityId);

        return R.ok();
    }


    @ApiOperation(value = "商品浏览")
    @ApiImplicitParam(value = "浏览实体id", name="postId", example = "2", required = true)
    @PostMapping ("/record/watch")
    public R<Void> recordWatch(@RequestBody Map<String, String> map){
        Long userId = SecurityContextUtils.getUserId();
        likeService.recordEntityWatch(userId, ENTITY_TYPE_POST, Integer.parseInt(map.get("postId")));
        return R.ok();
    }
    @ApiOperation(value = "商品想要记录")
    @ApiImplicitParam(value = "想要实体id", name="postId", example = "2", required = true)
    @PostMapping ("/record/want")
    public R<Void> recordWant(@RequestBody Map<String, String> map){
        Long userId = SecurityContextUtils.getUserId();
        likeService.recordEntityWant(userId, ENTITY_TYPE_POST, Integer.parseInt(map.get("postId")));
        return R.ok();
    }
}
